{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# zlib 数据压缩模块\n",
    "直接支持通用的数据打包和压缩格式：zlib，gzip，bz2，zipfile，以及 tarfile。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 压缩与解压字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:38:55.259738Z",
     "start_time": "2020-08-20T19:38:55.256624Z"
    }
   },
   "outputs": [],
   "source": [
    "import zlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:39:41.012418Z",
     "start_time": "2020-08-20T19:39:40.997892Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "41"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_data = b'witch which has which witches wrist watch'\n",
    "len(str_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:40:02.184787Z",
     "start_time": "2020-08-20T19:40:02.178289Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "37"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 压缩\n",
    "t = zlib.compress(str_data)\n",
    "len(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:41:05.823511Z",
     "start_time": "2020-08-20T19:41:05.817029Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'witch which has which witches wrist watch'\n",
      "b'x\\x9c+\\xcf,I\\xceP(\\xcf\\xc8\\x04\\x92\\x19\\x89\\xc5PV9H4\\x15\\xc8+\\xca,.Q(O\\x04\\xf2\\x00D?\\x0f\\x89'\n"
     ]
    }
   ],
   "source": [
    "print(str_data)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:42:38.713734Z",
     "start_time": "2020-08-20T19:42:38.710334Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'witch which has which witches wrist watch'\n"
     ]
    }
   ],
   "source": [
    "# 解压缩\n",
    "dec_data = zlib.decompress(t)\n",
    "print(dec_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:43:28.960734Z",
     "start_time": "2020-08-20T19:43:28.956745Z"
    }
   },
   "outputs": [],
   "source": [
    "a = zlib.crc32(str_data)\n",
    "b = zlib.crc32(dec_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:43:39.667788Z",
     "start_time": "2020-08-20T19:43:39.663795Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "226805979 226805979\n"
     ]
    }
   ],
   "source": [
    "print(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 压缩与解压文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T19:57:19.353894Z",
     "start_time": "2020-08-20T19:57:19.345159Z"
    }
   },
   "outputs": [],
   "source": [
    "import zlib\n",
    "def compress(infile, dst, level=9):\n",
    "    infile = open(infile, 'rb')\n",
    "    dst = open(dst, 'wb')\n",
    "    compress = zlib.compressobj(level)\n",
    "    data = infile.read(1024)\n",
    "    while data:\n",
    "        dst.write(compress.compress(data))\n",
    "        data = infile.read(1024)\n",
    "    dst.write(compress.flush())\n",
    "\n",
    "def decompress(infile, dst):\n",
    "    infile = open(infile, 'rb')\n",
    "    dst = open(dst, 'wb')\n",
    "    decompress = zlib.decompressobj()\n",
    "    data = infile.read(1024)\n",
    "    while data:\n",
    "        dst.write(decompress.decompress(data))\n",
    "        data = infile.read(1024)\n",
    "    dst.write(decompress.flush())\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    compress('in.txt', 'out.txt')\n",
    "    decompress('out.txt', 'out_decompress.txt')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
